#include "SceneNode.h"

mtBEGIN(SceneNodeDef)
	mtCONSTRUCTOR();
mtEND

mtBEGIN(SceneNode)
	mtBASE(ECS::Component);
	mtCONSTRUCTOR();
	mtMETHOD(initWith);
	mtMETHOD(onStart);
mtEND

SceneNode::SceneNode()
{
}

SceneNode::~SceneNode()
{
	_node->removeFromParentAndCleanup(true);
	CC_SAFE_RELEASE_NULL(_node);
}

void SceneNode::onStart()
{
	auto mainNode = getNode();
	_tClient = getComponent<Transform>()->createClient([mainNode](const ECS::Vec2<float>& pos, float ang)
	{
		mainNode->setPosition( ccp(pos.x * 64.0f, pos.y * 64.0f));
		mainNode->setRotation(90.0f - ang * 180.0f / 3.14f);
	});
}

cocos2d::CCNode* SceneNode::getNode() const
{
	return _node;
}

bool SceneNode::initWith( SceneNodeDef* def )
{
	_node = new cocos2d::CCNode();
	return true;
}

void SceneNode::onDestroy()
{
	_node->removeFromParentAndCleanup(true);
	CC_SAFE_RELEASE_NULL(_node);
}
